<?php
namespace app\admin\controller;
use app\index\model\NavModel;
use app\index\model\CtModel;
class Homemgr extends Base
{
	#-< NavCate >---------------------------------------------------------------
    public function navcate(){
		if(request()->isAjax()){
            $navModel = new NavModel();
            $navcate = $navModel::order(['sort asc','id asc'])->select()->toArray();
            $navcate = arr2tree($navcate);
            return ['code'=>1,'data'=>$navcate,'msg'=>'ok'];
        }
		return $this->fetch();
	}

	public function navcateAdd(){
		$param = input("post.");
		$navModel = new NavModel();
		$navModel::startTrans();
		try {
			$navModel->insert($param);
			$navModel::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (\Exception $e) {
			$navModel::rollback();
			return ['code'=>1,'msg'=>'添加失败:'.$e->getMessage()];
		}
	}

	public function navcateEdit(){
		$param = input("post.");
	    $navModel = new NavModel();
		$navModel::startTrans();
		try {
			$navModel->where('id',$param['id'])->update($param);
			$navModel::commit();
			return ['code'=>1,'msg'=>'编辑成功'];
		} catch (\Exception $e) {
			$navModel::rollback();
			return ['code'=>1,'msg'=>'编辑失败:'.$e->getMessage()];
		}
	}

	public function navcateDel(){
		$param = input("post.");
	    $navModel = new NavModel();
		$navModel::startTrans();
		try {
			$navModel->where('id',$param['id'])->delete();
			$navModel::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		} catch (\Exception $e) {
			$navModel::rollback();
			return ['code'=>1,'msg'=>'删除失败:'.$e->getMessage()];
		}
	}

	#-< ctmgr >-----------------------------------------------------------------
	public function ctmgr() {
		if(request()->isAjax()){
			$param = input("param.");
			$param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			$ctModel = new UserModel();
            $data['rows'] = $ctModel->where($where)->order('id desc')->select()->toArray();
			$data['total'] = $ctModel->where($where)->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		return $this->fetch();
	}

	public function ctAdd() {
		$param = input("post.");
	    $ctModel = new UserModel();
		$ctModel::startTrans();
		try {
			$ctModel->insert($param);
			$ctModel::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		} catch (\Exception $e) {
			$ctModel::rollback();
			return ['code'=>1,'msg'=>'添加失败:'.$e->getMessage()];
		}
	}

	public function ctEdit() {
		$param = input("post.");
	    $ctModel = new UserModel();
		$ctModel::startTrans();
		try {
			$ctModel->where('id',$param['id'])->update($param);
			$ctModel::commit();
			return ['code'=>1,'msg'=>'编辑成功'];
		} catch (\Exception $e) {
			$ctModel::rollback();
			return ['code'=>1,'msg'=>'编辑失败:'.$e->getMessage()];
		}
	}

	public function ctDel() {
		$param = input("post.");
	    $ctModel = new UserModel();
		$ctModel::startTrans();
		try {
			$ctModel->where('id',$param['id'])->delete();
			$ctModel::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		} catch (\Exception $e) {
			$ctModel::rollback();
			return ['code'=>1,'msg'=>'删除失败:'.$e->getMessage()];
		}
	}
}
